/*
ehs - 2010-03-25
user interface JavaScript functions
*/

function showPopup(popupName)
{
	var popup = document.getElementById(popupName + "Popup");
	if (popup == null)
		return;

	document.getElementById("overlay").style.visibility = "visible";
	hideAllPopups();
	popup.style.visibility = "visible";
}


function isPopupShowing(popupName)
{
	var popup = document.getElementById(popupName + "Popup");
	return popup != null && popup.style.visibility == "visible";
}


function clearPopup()
{
	document.getElementById("overlay").style.visibility = "hidden";
	hideAllPopups();
	removeAddressBar(); /* can i conditionally call this?  i want this function to work in both iPhone and desktop */
}


function hideAllPopups()
{
	var allPopups = document.getElementsByClassName("popup");
	for (i = 0; i < allPopups.length; ++i)
		allPopups.item(i).style.visibility = "hidden";
}


var lastPadClicked = null;


function updateRenameControls()
{
	if (lastPadClicked != null) {
		document.getElementById("renameTitle").innerHTML = "Previously: " + lastPadClicked.innerHTML;
		document.getElementById("renameText").value = lastPadClicked.innerHTML;
	}
	else {
		document.getElementById("renameTitle").innerHTML = "Select a pad to rename";
		document.getElementById("renameText").value = "";
	}
	// todo: enable/disable buttons (and maybe text edit)
}


function padClicked(pad)
{
	if (lastPadClicked != null)
		lastPadClicked.style.backgroundColor = ""; /* abstract this out */
	lastPadClicked = pad;
	if (pad == null)
		return;

	updateRenameControls()
	if (pad.innerHTML == "")
		showPopup("rename"); // might already be showing
	if (isPopupShowing("rename"))
		pad.style.backgroundColor = "lightblue"; /* abstract this out */
}


function renamePad()
{
	if (lastPadClicked != null)
		lastPadClicked.innerHTML = document.getElementById("renameText").value;
	removeAddressBar();
}


function doneRenaming()
{
	renamePad();
	if (lastPadClicked != null)
		lastPadClicked.style.backgroundColor = ""; /* abstract this out */
	clearPopup();
}

